import { apps } from "./app.js";

export const NOT_LOADED = "NOT_LOADED"; // 应用默认状态是未加载状态
export const LOADING_SOURCE_CODE = "LOADING_SOURCE_CODE"; // 正在加载文件资源
export const NOT_BOOTSTRAPPED = "NOT_BOOTSTRAPPED"; //此时没有调用 bootstrap
export const BOOTSTRAPPING = "BOOTSTRAPPING"; // 正在启动中，此时bootstrap调用完毕后，需要表示成没有挂载
export const NOT_MOUNTED = "NOT_MOUNTED"; //调用了mount方法
export const MOUNTED = "MOUNTED"; //表示挂载成功
export const UNMOUNTING = "UNMOUNTING"; //卸载中，卸载后回到NOT_MOUNTED

//当前应用是否被挂载了，状态是不是MOUNTED
export function isActive(app) {
  return app.status === MOUNTED;
}

//路径匹配到才会加载应用，返回true，就会加载
export function shouldBeActive(app) {
  return app.activeWhen(window.location);
}

export function getAppChanges() {
  const appsToLoad = []; //需要加载的列表
  const appsToMount = []; //需要挂载的列表
  const appsToUnmount = []; // 需要移除的列表

  apps.forEach((app) => {
    // 看下这个app是否要加载
    const appShouldBeActive = shouldBeActive(app);

    switch (app.status) {
      case NOT_LOADED:
      case LOADING_SOURCE_CODE:
        // 没有被加载就是要去加载的app，如果正在加载资源，说明也没有加载过
        if (appShouldBeActive) {
          appsToLoad.push(app);
        }
        break;
      case NOT_BOOTSTRAPPED:
      case NOT_MOUNTED:
        // 没启动过，并且没挂载，说明等会要去挂载
        if (appShouldBeActive) {
          appsToMount.push(app);
        }
        break;
      case MOUNTED:
        // 正在挂载中但是路径不匹配了，就是要卸载的
        if (!appShouldBeActive) {
          appsToUnmount.push(app);
        }
        break;
      default:
        break;
    }
  });

  return { appsToLoad, appsToMount, appsToUnmount };
}
